package com.liyongjian.xhban.dal;

import java.math.BigInteger;
import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import com.liyongjian.xhban.model.Group;
import com.liyongjian.xhban.model.emumm.BlogType;
import com.liyongjian.xhban.model.emumm.LiveDieState;
import com.liyongjian.xhban.model.emumm.YesNoState;

public interface GroupRepository
        extends PagingAndSortingRepository<Group, Long>, JpaSpecificationExecutor<Group> {

    int countByUpdUserIdAndTypeAndStateAndIsSealed(Long updUserId, BlogType type,
            LiveDieState state, YesNoState isSealed);

    List<Group> findByUpdUserIdAndState(Long updUserId, LiveDieState state);

    @Query(value = "select DISTINCT(g.id) from tb_group as g left join tb_account_group as t on g.id=t.group_id where (t.group_id=g.id or t.group_id is null) and ((t.user_id=:userId and t.state=1) or (g.upd_user_id=:userId)) and g.state=1",
            nativeQuery = true)
    List<BigInteger> findMyGroupByUserId(@Param("userId") Long userId);

}
